/* eslint-disable import/no-anonymous-default-export */
// reducer是state数据处理的过程，action就是指挥数据变化的过程，使用传入的action来更新state
import i18n from '../../i18n/configs';
import { ADD_LANGUAGE, CHANGE_LANGUAGE } from './language-acitons';

export interface LanguageState {
  language: 'en' | 'zh';
  languageList: { name: string; code: string }[];
}

const defaultState: LanguageState = {
  language: 'zh',
  languageList: [
    { name: '中文', code: 'zh' },
    { name: 'English', code: 'en' }
  ]
};
export default (state = defaultState, action) => {
  switch (action.type) {
    // 切换语言
    case CHANGE_LANGUAGE:
      i18n.changeLanguage(action.payload); // 这样处理是不标准的，有副作用
      return { ...state, language: action.payload };
    // 添加
    case ADD_LANGUAGE:
      return {
        ...state,
        languageList: [...state.languageList, action.payload],
        language: action.payload.code
      };
    default:
      return state;
  }
};
